package cn.com.dhc.danlu.widget.home;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.ToastUtil;

/**
 * @author: 李一方
 * @time: 2016-08-04 16:03
 * @mail: liyifang@danlu.com
 * @desc: 首页加入购物车购买选择数量的视图
 */
public class PurchaseView extends RelativeLayout {
    private Button btnSure;
    private NumEditext neCount;

    private OnSureClickListener onSureClickListener;

    public PurchaseView(Context context) {
        super(context);
        init();
    }

    public PurchaseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        setup();
    }

    public void setOnSureClickListener(OnSureClickListener onSureClickListener) {
        this.onSureClickListener = onSureClickListener;
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dl_item_home_purchase, this, true);
        btnSure = (Button) view.findViewById(R.id.btn_sure);
        neCount = (NumEditext) view.findViewById(R.id.ne_count);
    }

    public void setMaxCount(int maxCount) {
        neCount.setMaxCount(maxCount);
    }

    public void setMinCount(int minCount) {
        neCount.setMinCount(minCount);
    }

    public void setDefaultCount(int defaultCount) {
        neCount.setDefaultCount(defaultCount);
    }

    private void setup() {
        neCount.setOnOperationListener(new NumEditext.OnOperationListener() {
            @Override
            public void onReduce(View v) {
                if (null != onSureClickListener) {
                    onSureClickListener.onCountChanged(Integer.valueOf(neCount.getText().trim()));
                }
            }

            @Override
            public void onPlus(View v) {
                if (null != onSureClickListener) {
                    onSureClickListener.onCountChanged(Integer.valueOf(neCount.getText().trim()));
                }
            }

            @Override
            public void onCountChanged(int count) {
                if (null != onSureClickListener) {
                    onSureClickListener.onCountChanged(count);
                }
            }
        });
        btnSure.setOnClickListener(new NoRepeatClickListener() {
            @Override
            protected void onNoRepeatClick(View v) {
                if (null != onSureClickListener) {
                    String res = neCount.getText().trim();
                    if (TextUtils.isEmpty(res)) {
                        ToastUtil.getInstance().show("请输入购买数量");
                        return;
                    }
                    Long value = Long.parseLong(res);
                    if (value > neCount.getMaxCount()) {
                        ToastUtil.getInstance().show("购买数量超过999999999");
                        return;
                    }
                    if (value < neCount.getMinCount()) {
                        ToastUtil.getInstance().show("购买数量不能小于" + neCount.getMinCount());
                        return;
                    }
                    onSureClickListener.onClick(v, Integer.parseInt(neCount.getText().trim()));
                }
            }
        });
    }

    public interface OnSureClickListener {
        /**
         * 点击事件
         *
         * @param v
         *         视图
         * @param count
         *         购买数量
         */
        void onClick(View v, int count);

        void onCountChanged(int count);
    }
}